#!/bin/zsh

# Case statement could be edited to select desired log results to be echoed to user
# June 2020 Sean Holden
# Python script based upon: https://github.com/gregneagle/macaduk2017/blob/master/06_NotificationCenter/03_notificationcenter.py

tmp_python_file="/tmp/pyscript.py"

function show_message {

export thetitle="$the_title"
export thesubtitle="$the_subtitle"
export themessage="${line##*|}"
export thebundleid="$bundle_id"

cat << EOF > "$tmp_python_file"
#!/usr/bin/python
from Foundation import NSBundle
from Foundation import NSUserNotificationCenter
from Foundation import NSUserNotification

def display_bundleid(bundleid):
    bundle = NSBundle.mainBundle()
    info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
    info['CFBundleIdentifier'] = bundleid

def notify(title, subtitle, text, bundleid=None):
    if bundleid:
        display_bundleid(bundleid)
    notification = NSUserNotification.alloc().init()
    notification.setTitle_(title)
    notification.setSubtitle_(subtitle)
    notification.setInformativeText_(text)

    nc = NSUserNotificationCenter.defaultUserNotificationCenter()
    nc.deliverNotification_(notification)

notify(u'$thetitle', u'$thesubtitle', u'$themessage', bundleid='$thebundleid')
EOF

chmod 755 "$tmp_python_file"
"$tmp_python_file"
rm "$tmp_python_file"

}


while read line
do
	case "$line" in
		*"Model version"*|*"Downloading Fileset"*|*"Done activating"*|*"Activate all"*)
			show_message
			;;
		*"Data successfully sent to the inventory server"*)
			exit 0
			;;
	esac
done< <(tail -1 -f /private/var/log/fwcld.log) 

exit 0